package cn.tedu.gxtt.user.webapi.service;

import cn.tedu.gxtt.commons.ex.ServiceException;
import cn.tedu.gxtt.commons.pojo.user.dto.UserAddNewDTO;
import cn.tedu.gxtt.commons.pojo.vo.UserSimpleVO;
import cn.tedu.gxtt.commons.restful.ResponseCode;
import cn.tedu.gxtt.user.service.IUserService;
import cn.tedu.gxtt.user.webapi.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
@Repository
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 添加用户
     * @param userAddNewDTO
     */
    @Override
    public void addNew(UserAddNewDTO userAddNewDTO) {
        // 从参数中取出尝试添加的类别的名称
        String name = userAddNewDTO.getUsername();
        // 调用userMapper.getByName()方法查询
        UserSimpleVO user = userMapper.getByName(name);
        // 判断查询结果是否不为null
        if (user != null) {
//             是：抛出ServiceException
            throw new ServiceException(ResponseCode.ERR_USERNAME,"添加类别失败，名称（" + name + "）已存在！");
        }
    }
}
